<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dathang_model extends CI_Model { 
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$this->load->database();
    }
	function Them($data)
    {
        $this->db->insert('dondathang', $data);
		$result=$this->db->query("SELECT LAST_INSERT_ID();");
		$db=$result->row_array();
		return $db['LAST_INSERT_ID()'];
    }
	function Them_CT($data)
    {
        $this->db->insert('ddh_sp', $data);
		$result=$this->db->query("SELECT LAST_INSERT_ID();");
		$db=$result->row_array();
		return $db['LAST_INSERT_ID()'];
    }
	function Them_ThongTinThanhToan($data)
    {
        $this->db->insert('thongtinthanhtoan', $data);
		$result=$this->db->query("SELECT LAST_INSERT_ID();");
		$db=$result->row_array();
		return $db['LAST_INSERT_ID()'];
    }
	///////Tài Khoản///////////////////////////////////////////////////////////////
	
	function KiemTraTK($TenTK){
			$this->db->select("*");
			$this->db->where("TenTaiKhoan",$TenTK);
			$data = $this->db->get("taikhoan");
			return  $data->row_array(); 
		}
	function ThemTaiKhoan($data)
    {
     	$this->db->insert('taikhoan', $data);
		$result=$this->db->query("SELECT LAST_INSERT_ID();");
		$db=$result->row_array();
		return $db['LAST_INSERT_ID()'];  
    }
	function UpTaiKhoan($data)
    {
     	$this->db->where("MaTK",$data['MaTK']);
      	return $this->db->update('taikhoan', $data);  
    }
	
	function ThemTaiKhoan_Re($data)
    {
     	$this->db->insert('re_matkhau', $data);
		$result=$this->db->query("SELECT LAST_INSERT_ID();");
		$db=$result->row_array();
		return $db['LAST_INSERT_ID()'];  
    }
	
	/////////////////////////////////////////////////////////////////////////
	function UpThanhToan($data)
    {
     	$this->db->where("MaDDH",$data['MaDDH']);
      	return $this->db->update('dondathang', $data);  
    }
	
	function LuotDatHang($masp)
	{
		$this->db->select("SoLuong,soluotdathang,SoLuotXem");
		$this->db->where("masp",$masp);
        $query=$this->db->get("sanpham");
       	return $query->row_array();
	}
	
	function UpLuotDatHang($data)
    {
     	$this->db->where("MaSP",$data['MaSP']);
      	return $this->db->update('sanpham', $data);  
    }
	
	function LayDDH_MaDDH($MaDDH)
    {
     	$this->db->where("MaDDH",$MaDDH);
		$query =  $this->db->get('dondathang'); 
		return $query->row_array();  
    }
	function LayTTTT_DDH_MaDDH($Ma_DDH)// thông tin thanh toán
    {
     	$this->db->where("Ma_DDH",$Ma_DDH); 
		$query =  $this->db->get('thongtinthanhtoan'); 
		return $query->row_array();  
    }
	
	function LayCT_DDH_MaDDH($Ma_DDH)// đơn hàng
    {
     	$this->db->select('TenSanPham,DonGia,ddh_sp.SoLuong');
		$this->db->from('ddh_sp'); 
		$this->db->join('sanpham', 'ddh_sp.Ma_SP = sanpham.MaSP', 'left');
		$this->db->where("Ma_DDH",$Ma_DDH); 
		$query = $this->db->get(); 
		return $query->result_array();  
    }
	function Xoa($MaDDH) // Xóa đơn hàng
    {
       	$this->db->where('MaDDH', $MaDDH);
		$this->db->delete('dondathang');  
    }
	function Xoa_CT($Ma_DDH)// Xóa chi tiết đặt hàng
    {
     	$this->db->where('Ma_DDH', $Ma_DDH);
		$this->db->delete('ddh_sp');  
    }
	function Xoa_TT($Ma_DDH)// Xóa Thông tin thanh toán
    {
     	$this->db->where('Ma_DDH', $Ma_DDH);
		$this->db->delete('thongtinthanhtoan');  
    }
	function Xoa_TK($MaTK)
    {
     	$this->db->where('MaTK', $MaTK);
		$this->db->delete('taikhoan');  
    }
}